<?php

declare (strict_types=1);

namespace app\admin\controller;

use app\BaseController;
use app\common\controller\Backend;
use think\Exception;
use \think\facade\Cache;
use \app\admin\model\Category as CategoryModel;
use \app\admin\model\Album as AlbumModel;
use \app\admin\model\DocCon as DocConModel;
use think\facade\Db;
use think\facade\View;


class Recycle extends Backend
{
    public function initialize() {
        parent::initialize();
//        $this->_mod = Db::name('menu');
        $this->_mod  = new \app\admin\model\DocCon();
    }

    public function searchArr(){
        $where[]=array('del_status','=',2);
        $status = request()->param('status')?(int)request()->param('status'):-1;
        if($status >= 0) $where[]=array('status','=',$status);
        $cid = request()->param('cid')?(int)request()->param('cid'):-1;
        $catearr ='';
        if($cid >= 0) {
            $idarr = getcateallid($cid);
            if(is_array($idarr)){
                foreach ($idarr as $key =>$value){
//                    var_dump($value);exit;
//                    var_dump(is_array($idarr));exit;
                    if($key == 0){
                        $catearr = $value['id'];
                    } else{
                        $catearr.=','.$value['id'];
                    }
                }
            } else{
                $catearr = $idarr;
            }
            $where[]=array('cid','in',$catearr);
        }

        $title =  request()->param('title');
        if($title) $where[]=array('title','like','%'.$title.'%');
        View::assign('searchArr', array(
            'title'=>$title,
            'status' =>$status,
            'cid' =>$cid
        ));
        return $where;
    }


    public function _before_index(){
        $cate_mod =new CategoryModel();
        $where = $this->searchArr();
        $this->_list($this->_mod, $where, 'id', 'desc');
        $res = $cate_mod->field('id,name')->select();
        $cate_list = array();
        foreach ($res as $val) {
            $cate_list[$val['id']] = $val['name'];
        }
        View::assign('cate_list', $cate_list);
        $catelist = $cate_mod->where(array('pid'=>0,'status'=>1))->select();
        foreach ($catelist as $k => $v) {
            $catelist[$k]['erji'] = $cate_mod->where(array('pid'=>$v['id'],'status'=>1))->select();
        }
        View::assign('catelist', $catelist);
    }

}